#ifndef WES_MYMAP_HEADER
#define WES_MYMAP_HEADER

#include <sstream>
#include <map>
#include <utility> // for pair

template<class T> class MyMap : public std::map<std::string, T>
/*	The main use of this map is to get map options from a string where the pairs
are separated by semicolons (don't forget last semicolon.  For example:
		std::string s = "one 1;two 22;three 333.3;four 44ddd;";
*/
{
public:
	MyMap(){};
	MyMap(std::string s);
	MyMap(std::string s, T val) {(*this)[s] = val;}
	std::map<std::string, T> makemap(std::string s, T fake);
private:
	std::pair<std::string, T> getpair(std::string s, int *pstart, std::string sep, T fake);
	std::string getstring(std::string s, int *pstart, std::string sep);
};

template <class T> MyMap<T>::MyMap(std::string s)
{
//	(*this)["hello"] =777;
	std::map<std::string, T> m = makemap(s, T());
	typename std::map<std::string, T>::iterator it;
	for(it = m.begin(); it != m.end(); it++)
		(*this)[it->first] = it->second;
}

template <class T> std::map<std::string, T> MyMap<T>::makemap(std::string s, T fake)
{
	int start=0;
	std::map<std::string, T> m;
	std::pair<std::string, T>  mp;
	while ((mp=getpair(s, &start,";", fake)).first != "AllDone")
		m[mp.first] = mp.second;
	return m;
}
		
template <class T> std::pair<std::string, T> MyMap<T>::getpair(std::string s, int *pstart, std::string sep, T fake)
{
	std::pair<std::string, T> p("AllDone",fake);
	std::stringstream ss;
	ss << getstring(s,pstart,sep);
	ss >> p.first;
	ss >> p.second;
	return p;	
}
	
template <class T> std::string MyMap<T>::getstring(std::string s, int *pstart, std::string sep)
{
	int start =*pstart;
	int end = 0;
	end = (int) s.find(sep,start);
	if (end == -1)
		return "";
	*pstart = end +1;
	return s.substr(start,(end-start));
}


#endif


